package com.yinxin.IoStream;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

import java.beans.Visibility;
import java.io.*;

/**
 * @author yinxin
 * @Title:
 * @Package
 * @Description:
 * @date 2022/1/249:42
 */
public class TestFilePath {


    @Test
    public void testBuffer() throws IOException {
        try {
            run();
            System.out.println("输出");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void run() throws Exception {
        throw new Exception("报错信息");
    }

    @Test
    public void test() {
        System.out.println("D:\\Code\\MyStudy\\JavaSE\\images\\1.jpg");
    }

    @Test
    public void TestCreateFile() throws IOException {
        File fileInput = new File("D:\\Code\\MyStudy\\JavaSE\\images", "4.jpg");
        InputStream input = new FileInputStream(fileInput);
        File fileOut = new File("D:\\Code\\MyStudy\\JavaSE\\images\\store\\4.jpg");
        OutputStream output = new FileOutputStream(fileOut);
        // 建立缓冲区
        byte[] bytes = new byte[10];
        int len;
        // 读不到东西read返回-1
        long start = System.currentTimeMillis();
        if ((len = input.read(bytes)) != -1) {
            output.write(bytes, 0, len);
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        output.flush();
        input.close();
        output.close();
    }

    @Test
    public void test11(){
        String s = "[communityCount: 11, ranIterations: 1, didConverge: TRUE]";
        String[] statsArr = s.substring(1, s.length() - 1).replaceAll("NaN", "null").replaceAll(" ","").split(",");
        for (String s1 : statsArr) {
            String[] split = s1.split(":");

            System.out.println();
        }
//        System.out.println(jsonObject);
//        JSONArray objects = JSON.parseArray(s);
//        System.out.println(objects);
    }

    // AutoClose接口实现try...catch...自动关闭资源
    @Test
    public void TestCopyFile() {
        try(InputStream input = new FileInputStream("D:\\Code\\MyStudy\\JavaSE\\images\\1.jpg");
             OutputStream output = new FileOutputStream("D:\\Code\\MyStudy\\JavaSE\\images\\store\\1.jpg")) {
            // 建立缓冲区
            byte[] bytes = new byte[10];
            // 读不到东西read返回-1
            long start = System.currentTimeMillis();
            while ( input.read(bytes) != -1) {
                output.write(bytes);
            }
            long end = System.currentTimeMillis();
            System.out.println(end - start);
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

}
